package cn.cvs.interceptor;

import cn.cvs.pojo.SysUser;
import cn.cvs.util.Constants;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 配置登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object loginUser = request.getSession().getAttribute(Constants.USER_SESSION);//获取用户
        //判断他是否为空
        if (loginUser!=null) {
            return true;//将请求放行给请求
        }
        String contextPath = request.getContextPath();//动态工厂名
        response.sendRedirect(contextPath+"/toLogin");//重定向返回登录
        return false;
    }

}
